﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spc;
using Spc.Web;
using System.Text.RegularExpressions;
using System.Web;

namespace EasyB2C
{
    public static class Request
    {
        public static void app_OnPortalRequest(CmsContext t, ref bool handled)
        {
        }


        public static void app_OnCategoryPageRequest(CmsContext t, string t1, int page,ref bool b)
        {

        }


        public static void app_OnArchivePagePost(CmsContext t, string allhtml, ref bool handled)
        {
        	const string pattern="/((.+)/)*([^.]+).html";
        	
        	if(Regex.IsMatch(allhtml,String.Concat(Config.AccessPrefix,pattern),RegexOptions.IgnoreCase))
        	{
        		Match m=Regex.Match(allhtml,String.Concat(Config.AccessPrefix,pattern),RegexOptions.IgnoreCase);
        		var action=m.Groups[3].Value;
        		
        		/// <summary>
        		/// 保存状态
        		/// </summary>
        		/// <param name="t"></param>
        		/// <param name="allhtml"></param>
        		/// <param name="handed"></param>
        		if(action=="require")
        		{
        			float totalFee=float.Parse(t.Request["total_fee"]);
        			string orderContent=t.Request["order_content"];
        			string orderExt=t.Request["order_ext"];
        			
        			if(totalFee!=0 && orderContent!="" && orderExt!="")
        			{
	        			t.HttpContext.Session["order"]=new b2c.Order
	        			{
	        				TotalFee=totalFee,
	        				OrderContent=orderContent,
	        				OrderExt=orderExt
	        			};
	        		
	        			t.Response.Write("{result:true,message:''}");
        			}
        			else
        			{
        				t.Response.Write("{result:false,message:'订单无效'}");
        			}
        			
        			handled=true;
        		}else{
        			t.Response.Write(action);
        			handled=true;
        		}
        	}
        }

        public static void app_OnArchivePageRequest(CmsContext t, string allhtml, ref bool handed)
        {
        	const string pattern="/((.+)/)*([^.]+).html";
        	
        	if(Regex.IsMatch(allhtml,String.Concat(Config.AccessPrefix,pattern),RegexOptions.IgnoreCase))
        	{
        		Match m=Regex.Match(allhtml,String.Concat(Config.AccessPrefix,pattern),RegexOptions.IgnoreCase);
        		var action=m.Groups[3].Value;
        		var module=m.Groups[2].Value;
        		string html;
        		
        		
        		if(action=="order")
        		{
        			html = PageUtility.Require(String.Format("/{0}/easyb2c_order{1}", t.CurrentSite.Tpl,Config.IsMobileAccess?"_mobile":""), null,
	                     tpl =>
	                     {
	                         tpl.AddVariable<PageVariable>("page", new PageVariable { Title = "订单", Keywords = "", Description = "" });
	                     }
	                     );
	
	                t.Render(html);
        		
        			handed=true;
        		}
        		else if(action=="order_result")
        		{
        			
        			b2c.Order order=t.HttpContext.Session["order"] as b2c.Order;
        			if(order==null)
        			{
        				t.Response.Write("<script>location.replace('/')</script>");
        			}
        			else
        			{
        			
	        			html = PageUtility.Require(String.Format("/{0}/easyb2c_order_result{1}", t.CurrentSite.Tpl,Config.IsMobileAccess?"_mobile":""), null,
		                     tpl =>
		                     {
		                         tpl.AddVariable<PageVariable>("page", new PageVariable { Title = "订单确认", Keywords = "", Description = "" });
		                        
		                         
	
		                         tpl.AddVariable("order",new{
		                                         	totalfee=order.TotalFee,
		                                         	content=order.OrderContent,
		                                         	ext=order.OrderExt
		                                         });
		                     }
		                     );
		
		                t.Render(html);
        			}
        			handed=true;
        		}
        		else if(module=="page")
        		{
        			html = PageUtility.Require(String.Format("/{0}/easyb2c_page_{1}{2}", t.CurrentSite.Tpl, action, Config.IsMobileAccess?"_mobile":""), null,
		                     tpl =>
		                     {
		                         tpl.AddVariable<PageVariable>("page", new PageVariable { Title = "EasyB2C", Keywords = "", Description = "" });
		                     }
		                     );
		
		                t.Render(html);
		                
		                handed=true;
        		}
        		
        		
        		
        	}
        }
    }
}
